<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        var arr = [{
            id: 1,
            name: 'candy',
            value: 40
        }, {
            id: 2,
            name: 'Simon',
            value: 50
        }, {
            id: 3,
            name: 'Tony',
            value: 45
        }, {
            id: 4,
            name: 'Annie',
            value: 60
        }];
        //将上面数组分别按元素对象的value值从小到大进行排序后输出


        arr.sort(function (a, b) {
            return b.value - a.value;
        })
        console.log(arr);


        let obj = {
            '未完成': 5,
            '已完成': 8,
            '待确认': 4,
            '已取消': 6
        };
        var arr = []
        for (let i in obj) {
            arr.push(obj[i]); //属性
            //arr.push(obj[i]); //值
        }
        console.log(arr);

        var scoreObject = {
            "Tony": {
                "Math": 95,
                "English": 79,
                "Music": 68
            },
            "Simon": {
                "Math": 100,
                "English": 95,
                "Music": 98
            },
            "Annie": {
                "Math": 54,
                "English": 65,
                "Music": 88
            }
        }
        //如上有一个用来存储学习成绩的对象，编写一个函数，将其转为如下的二维数组

        // var scoreArray = [
        //     ["Tony", 95, 79, 68],
        //     ……
        // ];
        var arr1 = []

        //console.log(scoreObject['Tony']['Math']);
        // scoreObject['Tony']['Math']
        // scoreObject['Tony']["English"]
        // scoreObject['Tony']["Music"]

        for (var i in scoreObject) {
            var arr2 = [];
            arr2.push(i);

            for (var j in scoreObject[i]) {

                arr2.push(scoreObject[i][j]);

            }
            arr1.push(arr2);
        }
        console.log(arr1);

        //数组转为对象： 
        var menuArr = [
            [1, "Area1", -1],
            [2, "Area2", -1],
            [3, "Area1-1", 1],
            [4, "Area1-2", 1],
            [5, "Area2-1", 2],
            [6, "Area2-2", 2],
            [7, "Area1-2-3", 4],
            [8, "Area2-2-1", 6],
        ];
        var menuObject = {};
        menuArr.forEach((current, index) => {
            if (current[2] == -1) {
                menuObject[current[0]] = {
                    "name": current[1],
                    "subMenu": {}
                }
            }
            if (current[2] == 1) {
                menuObject["1"]["subMenu"][current[0]] = {
                    "name": current[1],
                    "subMenu": {}
                }
            }
            if (current[2] == 2) {
                menuObject["2"]["subMenu"][current[0]] = {
                    "name": current[1],
                    "subMenu": {}
                }
            }
            if (current[2] == 4) {
                menuObject["1"]["subMenu"]["4"]["subMenu"][current[0]] = {
                    "name": current[1],
                    "subMenu": {}
                }
            }
            if (current[2] == 6) {
                menuObject["2"]["subMenu"]["6"]["subMenu"][current[0]] = {
                    "name": current[1],
                    "subMenu": {}
                }
            }
        })
        console.log(menuObject); 
    </script>
</body>

</html>